package cn.iocoder.yudao.module.member.service.faceanalyseresult;

import cn.iocoder.yudao.module.member.dal.dataobject.faceanalyseresult.FaceanalyseResultDO;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jayway.jsonpath.JsonPath;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.member.controller.admin.faceanalyseresult.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.faceanalyseresult.FaceanalyseResultDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.member.dal.mysql.faceanalyseresult.FaceanalyseResultMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.*;

/**
 * 会员皮肤状态分析结果 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class FaceanalyseResultServiceImpl implements FaceanalyseResultService {

    @Resource
    private FaceanalyseResultMapper faceanalyseResultMapper;

    @Override
    public Long createFaceanalyseResult(FaceanalyseResultSaveReqVO createReqVO) {
        // 插入
        FaceanalyseResultDO faceanalyseResult = BeanUtils.toBean(createReqVO, FaceanalyseResultDO.class);
        faceanalyseResultMapper.insert(faceanalyseResult);
        // 返回
        return faceanalyseResult.getId();
    }

    @Override
    public void updateFaceanalyseResult(FaceanalyseResultSaveReqVO updateReqVO) {
        // 校验存在
        validateFaceanalyseResultExists(updateReqVO.getId());
        // 更新
        FaceanalyseResultDO updateObj = BeanUtils.toBean(updateReqVO, FaceanalyseResultDO.class);
        faceanalyseResultMapper.updateById(updateObj);
    }

    @Override
    public void deleteFaceanalyseResult(Long id) {
        // 校验存在
        validateFaceanalyseResultExists(id);
        // 删除
        faceanalyseResultMapper.deleteById(id);
    }

    private void validateFaceanalyseResultExists(Long id) {
        if (faceanalyseResultMapper.selectById(id) == null) {
            throw exception(FACEANALYSE_RESULT_NOT_EXISTS);
        }
    }

    @Override
    public FaceanalyseResultDO getFaceanalyseResult(Long id) {
        StringBuffer result1=new StringBuffer();
        FaceanalyseResultDO result=faceanalyseResultMapper.selectById(id);
        String image_quality =JsonPath.read(result.getResult(), "$.image_quality").toString();
        System.out.println("image_quality-------------------------------------------"+image_quality);
        //肤质分析
        String skin_type =JsonPath.read(result.getResult(), "$.skin_type").toString();
        String skin_type_detail =JsonPath.read(result.getResult(), "$.skin_type.details").toString();

        System.out.println("skin_type_detail-------------------------------------------"+skin_type_detail);
        String skin_age =JsonPath.read(result.getResult(), "$.skin_age.value").toString();
        String oily_intensity =JsonPath.read(result.getResult(), "$.oily_intensity").toString();
        String water =JsonPath.read(result.getResult(), "$.water").toString();
        //肤色分析
        String skintone =JsonPath.read(result.getResult(), "$.skintone").toString();
        String skintone_ita=JsonPath.read(result.getResult(), "$.skintone_ita").toString();
        String skin_hue_ha=JsonPath.read(result.getResult(), "$.skin_hue_ha").toString();


        String blackhead=JsonPath.read(result.getResult(), "$.blackhead").toString();
        String blackhead_count=JsonPath.read(result.getResult(), "$.blackhead_count").toString();
//        String skin_hue_ha=JsonPath.read(result.getResult(), "$.skin_hue_ha").toString();
//        String skin_hue_ha=JsonPath.read(result.getResult(), "$.skin_hue_ha").toString();
        return result;
    }
    @Override
    public String getFaceanalyseResultStr(Long id) {
        StringBuffer result1=new StringBuffer("检测结果：");
        FaceanalyseResultDO result=faceanalyseResultMapper.selectById(id);
        String image_quality =JsonPath.read(result.getResult(), "$.image_quality").toString();
        String image_quality_glasses =JsonPath.read(result.getResult(), "$.image_quality.glasses").toString();
        if(image_quality_glasses.equals("0")){
            result1.append("无眼镜");
        }else {
            result1.append("有眼镜");
        }

        //肤质分析
        String skin_type =JsonPath.read(result.getResult(), "$.['skin_type']").toString();
        String skin_type_detail0 =JsonPath.read(result.getResult(), "$.['skin_type'].['details'].['0'].['confidence']").toString();
        String skin_type_detail1 =JsonPath.read(result.getResult(), "$.['skin_type'].['details'].['1'].['confidence']").toString();
        String skin_type_detail2 =JsonPath.read(result.getResult(), "$.['skin_type'].['details'].['2'].['confidence']").toString();
        String skin_type_detail3=JsonPath.read(result.getResult(), "$.['skin_type'].['details'].['3'].['confidence']").toString();
        result1.append("-肤质分析:");
        result1.append("油性皮肤:"+skin_type_detail0);
        result1.append("干性皮肤:"+skin_type_detail1);
        result1.append("中性皮肤:"+skin_type_detail2);
        result1.append("混合型皮肤:"+skin_type_detail3);

        String oily_intensity =JsonPath.read(result.getResult(), "$.oily_intensity").toString();
        String t_zone =JsonPath.read(result.getResult(), "$.oily_intensity.t_zone").toString();
        String left_cheek =JsonPath.read(result.getResult(), "$.oily_intensity.left_cheek").toString();
        String right_cheek =JsonPath.read(result.getResult(), "$.oily_intensity.right_cheek").toString();
        String chin_area =JsonPath.read(result.getResult(), "$.oily_intensity.chin_area").toString();
        String full_face =JsonPath.read(result.getResult(), "$.oily_intensity.full_face").toString();
        result1.append("-油性皮肤强度:");
        result1.append("-T区"+t_zone);
        result1.append("-左脸颊"+left_cheek);
        result1.append("-右脸颊"+left_cheek);
        result1.append("-下巴"+chin_area);
        result1.append("-全脸"+full_face);

        String water =JsonPath.read(result.getResult(), "$.water").toString();
        String water_severity =JsonPath.read(result.getResult(), "$.water.water_severity").toString();
        String water_area =JsonPath.read(result.getResult(), "$.water.water_area").toString();
        String water_forehead =JsonPath.read(result.getResult(), "$.water.water_forehead").toString();
        String water_leftcheek =JsonPath.read(result.getResult(), "$.water.water_leftcheek").toString();
        String water_rightcheek =JsonPath.read(result.getResult(), "$.water.water_rightcheek").toString();
        result1.append("-肤质分析:");
        result1.append("-肌肤水质强度"+water_severity);
        result1.append("-水敏感区域"+water_area);
        result1.append("-水敏感区域-前脸"+water_forehead);
        result1.append("-水敏感区域-左脸颊"+water_leftcheek);
        result1.append("-水敏感区域-右脸颊"+water_rightcheek);

        //肤色分析
        String skintone =JsonPath.read(result.getResult(), "$.skintone").toString();
        String skintone_ita=JsonPath.read(result.getResult(), "$.skintone_ita").toString();
        String skin_hue_ha=JsonPath.read(result.getResult(), "$.skin_hue_ha").toString();
        result1.append("-肤色分析:"+skintone_ita);
        result1.append("-肤色分析"+skintone_ita);
        result1.append("-肤色分析"+skin_hue_ha);

        String blackhead=JsonPath.read(result.getResult(), "$.blackhead").toString();
        String blackhead_count=JsonPath.read(result.getResult(), "$.blackhead_count").toString();
        String enlarged_pore_count=JsonPath.read(result.getResult(), "$.enlarged_pore_count").toString();
        String pores_forehead=JsonPath.read(result.getResult(), "$.pores_forehead").toString();
        String pores_right_cheek=JsonPath.read(result.getResult(), "$.pores_right_cheek").toString();
        String pores_left_cheek=JsonPath.read(result.getResult(), "$.pores_left_cheek").toString();
        String pores_jaw=JsonPath.read(result.getResult(), "$.pores_jaw").toString();
        String rough=JsonPath.read(result.getResult(), "$.rough").toString();
        result1.append("-粗糙程度分析:");
        result1.append("-黑头:"+blackhead);
        result1.append("-黑头数量:"+blackhead_count);
        result1.append("-毛孔数量:"+enlarged_pore_count);
        result1.append("-毛孔位置-前脸:"+pores_forehead);
        result1.append("-毛孔位置-右脸颊:"+pores_right_cheek);
        result1.append("-毛孔位置-左脸颊:"+pores_left_cheek);
        result1.append("-毛孔位置-下巴:"+pores_jaw);
        result1.append("-粗糙程度:"+rough);

        String melanin=JsonPath.read(result.getResult(), "$.melanin").toString();
        result1.append("-色素沉着分析:");
        result1.append("-色素:"+melanin);

        String acne=JsonPath.read(result.getResult(), "$.acne").toString();
        result1.append("-痤疮分析:");
        result1.append("-痤疮分析:"+acne);


        String sensitivity=JsonPath.read(result.getResult(), "$.sensitivity").toString();
        result1.append("-敏感分析:");
        result1.append("-敏感分析:"+sensitivity);


        String skin_age=JsonPath.read(result.getResult(), "$.skin_age").toString();
        result1.append("-衰老分析:");
        result1.append("-衰老分析:"+skin_age);

        String eye_pouch=JsonPath.read(result.getResult(), "$.eye_pouch").toString();
        result1.append("-烟袋分析:");
        result1.append("-烟袋分析:"+eye_pouch);
        return result1.toString();
    }

    @Override
    public PageResult<FaceanalyseResultRespVO> getFaceanalyseResultPage(FaceanalyseResultPageReqVO pageReqVO) {
        return faceanalyseResultMapper.selectPage(pageReqVO);
    }

}